﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Paging : System.Web.UI.UserControl
{
    public string CssClass
    {
        set
        {
            divPaging.Attributes.Remove("class");
            divPaging.Attributes.Add("class", value);
        }
    }

    protected int _PageSize;
    public int PageSize
    {
        get { return _PageSize; }
        set { _PageSize = value; }
    }

    protected bool _IsPaging = true;
    public bool IsPaging
    {
        get { return _IsPaging; }
        set { _IsPaging = value; }
    }

    protected int _TotalItems;
    public int TotalItems
    {
        get { return _TotalItems; }
        set { _TotalItems = value; }
    }

    protected int _CurrentPage;
    public int CurrentPage
    {
        get { return _CurrentPage; }
        set { _CurrentPage = value; }
    }

    public void BeginPaging()
    {
        if (this.Visible)
        {
            if (_PageSize == 0) _PageSize = 15;
            if (_CurrentPage == 0) _CurrentPage = 1;

            rptPaging.DataSource = HNG.Utilities.Common.CommonUtils.ItemsPaging(_CurrentPage, _PageSize, _TotalItems);
            rptPaging.DataBind();
        }
    }

    public void PagingProduct(int page, int size, int totalpage)
    {
        CurrentPage = page;
        _PageSize = size;
        _TotalItems = totalpage;
        BeginPaging();
    }

    protected void rptPaging_OnItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DataRowView dr = e.Item.DataItem as DataRowView;
        LinkButton lnkPaging = e.Item.FindControl("lnkPaging") as LinkButton;

        if (lnkPaging != null && dr != null)
        {
            lnkPaging.Text = dr["Text"].ToString();
            lnkPaging.CommandArgument = dr["Argument"].ToString();
            lnkPaging.Enabled = bool.Parse(dr["Enable"].ToString());

            if (lnkPaging.Enabled)
            {
                lnkPaging.CssClass = "item-page";
            }
            else
            {
                lnkPaging.CssClass = "currentpage";
            }
        }
    }

    public delegate void CommandHandler(object sender, EventArgs e);
    public event CommandHandler PageIndexChanged;
    protected void rptPaging_OnItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        int page;
        if (int.TryParse(e.CommandArgument.ToString(), out page))
        {
            _CurrentPage = page;
            if (PageIndexChanged != null)
            {
                PageIndexChanged(this, new EventArgs());
            }
        }
    }
}